﻿
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace DataLibrary.Utility
{
    public class ImageHelper
    {
        /// <summary> 
        /// 下载网站图片 
        /// </summary> 
        /// <param name="picUrl"></param> 
        /// <param path="设置保存目录"></param> 
        /// <param fileName="自定义文件名"></param> 
        /// <returns></returns> 
        public static string SaveAsWebImg(string path, string picUrl, string fileName)
        {
            string result = "";
            //设置保存目录

            //不存在目录则创建
            if (!Directory.Exists(path))
            {
                //创建目录
                Directory.CreateDirectory(path);
            }
            try
            {
                //判断图片是否为空或者null
                if (!String.IsNullOrEmpty(picUrl))
                {
                    //伪随机数生成器
                    Random rd = new Random();
                    //获取当前日期时间
                    DateTime nowTime = DateTime.Now;
                    //获取URL扩展名s
                    var Extension = Path.GetExtension(picUrl);
                    //自定义文件名
                    fileName += Extension;
                    WebClient webClient = new WebClient();
                    //下载url链接文件，并指定到本地的文件夹路径和文件名称
                    webClient.DownloadFile(picUrl, path + fileName);
                    //返回结果
                    result = fileName;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }



    }
}

